/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package NetworkPackage;

import FrameWork.Item;

/**
 *
 * @author mostafaizz
 */
public class XMLHandler {
    static XMLHandler instance;
    XMLHandler()
    {};
    public static XMLHandler getInstance()
    {
        if(instance == null)
        {
         instance = new XMLHandler();
        }
        return instance;
    }
    public String buildXML(Item item)
    {
        String ret = new String("<Item>");
        //
        ret += "<type>";
        ret += item.getType();
        ret += "</type>";
        //
        ret += "<ID>";
        ret += item.getID();
        ret += "</ID>";
        //
        ret += "<senderID>";
        ret += item.getSenderID();
        ret += "</senderID>";
        //
        ret += "<receiverID>";
        ret += item.getReceiverID();
        ret += "</receiverID>";
        //
        ret += "<Data>";
        ret += item.getData();
        ret += "</Data>";
        //
        ret += "<encryptionEnabled>";
        ret += item.getEncryptionEnabled();
        ret += "</encryptionEnabled>";
        //
        ret += "<encrypted>";
        ret += item.getEncrypted();
        ret += "</encrypted>";

        ret += "</Item>";
        return ret;
    }
    public Item buildItemFromXML(String msg)
    {
        Item item = new Item();
        String tmp = new String();
        if(msg.indexOf("<Item>") < 0)
            return item;
        int start = 0,end= 0;
        //fields
        // type
        start = msg.indexOf("<type>");
        end = msg.indexOf("</type>");
        if(start < 0 || end < 0)
        {
            return null;    // error
        }
        item.setType(Integer.parseInt(msg.substring(start+("<type>").length(), end)));
        // ID
        start = msg.indexOf("<ID>");
        end = msg.indexOf("</ID>");
        if(start < 0 || end < 0)
        {
            return null;    // error
        }
        item.setID(Integer.parseInt(msg.substring(start+("<ID>").length(), end)));
        // senderID
        start = msg.indexOf("<senderID>");
        end = msg.indexOf("</senderID>");
        if(start < 0 || end < 0)
        {
            return null;    // error
        }
        item.setSenderID(Integer.parseInt(msg.substring(start+("<senderID>").length(), end)));
        // receiverID
        start = msg.indexOf("<receiverID>");
        end = msg.indexOf("</receiverID>");
        if(start < 0 || end < 0)
        {
            return null;    // error
        }
        item.setReceiverID(Integer.parseInt(msg.substring(start+("<receiverID>").length(), end)));
        // encryptionEnabled
        start = msg.indexOf("<encryptionEnabled>");
        end = msg.indexOf("</encryptionEnabled>");
        if(start < 0 || end < 0)
        {
            return null;    // error
        }
        item.setEncryptionEnabled(msg.substring(start+("<encryptionEnabled>").length(), end).equalsIgnoreCase("true"));
        // encrypted
        start = msg.indexOf("<encrypted>");
        end = msg.indexOf("</encrypted>");
        if(start < 0 || end < 0)
        {
            return null;    // error
        }
        item.setEncrypted(msg.substring(start+("<encrypted>").length(), end).equalsIgnoreCase("true"));
        
        // Data
        start = msg.indexOf("<Data>");
        end = msg.indexOf("</Data>");
        if(start < 0 || end < 0)
        {
            return null;    // error
        }
        item.setData(msg.substring(start+("<Data>").length(), end));
          
        return item;
    }
}
